package com.krlin.word.word.service.impl;

import com.krlin.word.word.pojo.WordEntity;
import com.krlin.word.word.service.WordRedisService;
import com.krlin.word.word.utils.BeansUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author krlin
 */
@Service
public class WordRedisServiceImpl implements WordRedisService {

    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<WordEntity> getAllWords() {
        Object wordEntityList = redisTemplate.opsForValue().get("wordEntityJson");
        return BeansUtils.castToList(wordEntityList, WordEntity.class);
    }

    @Override
    public WordEntity getOneWordFromAllWords() {
        return (WordEntity) redisTemplate.opsForSet().randomMember("wordEntitySet");
    }

    @Override
    public List<WordEntity> getWordsByType(String type) {
        Object wordEntityList = redisTemplate.opsForValue().get("wordEntityJsonOf" + type);
        return BeansUtils.castToList(wordEntityList, WordEntity.class);
    }

    @Override
    public WordEntity getOneWordFromCertainType(String type) {
        return (WordEntity) redisTemplate.opsForSet().randomMember("wordEntitySetOf" + type);
    }
}
